<?php

namespace support\Exception\handler;

use Throwable;
use support\Exception\ApiException;
use Webman\Http\Request;
use Webman\Http\Response;
use Tinywan\Jwt\Exception\JwtRefreshTokenExpiredException;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;

class ExceptionHandler extends \Webman\Exception\ExceptionHandler
{
    public function report(Throwable $exception)
    {
        // TODO: Implement report() method.
    }

    public function render(Request $request, Throwable $exception): Response
    {
        //处理token异常
        if ($exception instanceof JwtTokenException || $exception instanceof JwtTokenExpiredException || $exception instanceof JwtRefreshTokenExpiredException) { //token校验错误
            return error(403, $exception->getMessage());
        }
        //处理程序异常
        if ($exception instanceof \ErrorException) {
            return error((int)$exception->getCode() ?? -1, $exception->getMessage(), [], config('app.debug') ? ['file' => $exception->getFile() . ':' . $exception->getLine()] : []);
        }
        //处理接口异常
        if ($exception instanceof ApiException || $exception instanceof \PDOException) {
            return error((int)$exception->getCode() ?? -1, $exception->getMessage(), [], config('app.debug') ? ['file' => $exception->getFile() . ':' . $exception->getLine()] : []);
        }
        // 处理其他异常
        return parent::render($request, $exception);
    }
}
